package com.caochenlei.tree.huffman;

import java.util.Arrays;
import java.util.Map;

public class HuffmanCodeTest {
    public static void main(String[] args) {
        String src = "i like java";
        byte[] sourceBytes = src.getBytes();

        System.out.println("解压前显示数据：" + src);
        System.out.println("解压前字节数组：" + Arrays.toString(sourceBytes));

        HuffmanTree huffmanTree = new HuffmanTree(sourceBytes);
        HuffmanCode huffmanCode = new HuffmanCode(huffmanTree);
        Map<Byte, String> huffmanCodes = huffmanCode.buildHuffmanCode();

        byte[] targetBytes = huffmanCode.encode(sourceBytes, huffmanCodes);
        System.out.println("压缩后字节数组：" + Arrays.toString(targetBytes));
        sourceBytes = huffmanCode.decode(targetBytes);
        System.out.println("解压后字节数组：" + Arrays.toString(sourceBytes));

        System.out.print("解压后显示数据：");
        for (byte sourceByte : sourceBytes) {
            System.out.print(new String(new byte[]{sourceByte}));
        }
    }
}
